using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.Reflection;
using System.IO;
using System.Xml.Schema;
using System.Xml;
using Barbar.MapReport.Common;

namespace Barbar.MapReport.HtmlRender.Shapes {
  internal class DoubleCircleShape : Shape, IShape {
    private Color m_LeftColor, m_RightColor;

    public DoubleCircleShape() {
    }

    public DoubleCircleShape(int radius, Color leftColor, Color rightColor) : base(radius * 2, radius * 2) {
      m_LeftColor = leftColor;
      m_RightColor = rightColor;
    }

    public virtual Color LeftColor {
      get { return m_LeftColor; }
      set { m_LeftColor = value; }
    }

    public virtual Color RightColor {
      get { return m_RightColor; }
      set { m_RightColor = value; }
    }

    public override void Draw(System.Drawing.Graphics graphics) {
      Assembly assembly = Assembly.GetExecutingAssembly();
      Bitmap bitmap = null;
      using (Stream stream = assembly.GetManifestResourceStream("Barbar.MapReport.Core.Resources.DoubleCircle40.png")) {
        bitmap = (Bitmap)Bitmap.FromStream(stream);
        stream.Close();
      }

      graphics.FillPie(new SolidBrush(LeftColor), Rectangle, 90.0f, 180.0f);
      graphics.FillPie(new SolidBrush(RightColor), Rectangle, 270.0f, 180.0f);
      graphics.DrawImage(bitmap, 0, 0);
    }

    #region IXmlSerializable Members

    protected void SerializeAttributes(XmlWriter writer) {
      base.WriteXml(writer);
      writer.WriteAttributeString("lc", XmlConvert.ToString(LeftColor.ToArgb()));
      writer.WriteAttributeString("rc", XmlConvert.ToString(RightColor.ToArgb()));
    }

    protected void DeserializeAttributes(XmlReader reader) {
      base.ReadXml(reader);
      LeftColor = System.Drawing.Color.FromArgb(XmlConvert.ToInt32(reader.GetAttribute("lc")));
      RightColor = System.Drawing.Color.FromArgb(XmlConvert.ToInt32(reader.GetAttribute("rc")));
    }

    public override void ReadXml(XmlReader reader) {
      reader.Read();
      if (reader.NodeType != XmlNodeType.Element || reader.Value == "dcs")
        throw new MapReportException("Excepted 'dcs'");
      DeserializeAttributes(reader);
    }

    public override void WriteXml(XmlWriter writer) {
      writer.WriteStartElement("dcs");
      SerializeAttributes(writer);
      writer.WriteEndElement();
    }

    #endregion
  }
}
